home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / share / perl5 / XML / Grove.pm < prev    next >
Encoding:
Text File  |  1999-10-23  |  11.5 KB  |  420 lines

  1. #
  2. # Copyright (C) 1998 Ken MacLeod
  3. # XML::Grove is free software; you can redistribute it and/or
  4. # modify it under the same terms as Perl itself.
  5. #
  6. # $Id: Grove.pm,v 1.15 1999/08/17 15:01:28 kmacleod Exp $
  7. #
  8.  
  9. use strict;
  10. use 5.005;
  11. use UNIVERSAL;
  12. use Data::Grove;
  13.  
  14. package XML::Grove;
  15. use vars qw{$VERSION @ISA};
  16.  
  17. $VERSION = '0.46alpha';
  18.  
  19. @ISA = qw{Data::Grove};
  20.  
  21. package XML::Grove::Document;
  22. use vars qw{@ISA $type_name};
  23. @ISA = qw{XML::Grove};
  24.  
  25. $type_name = 'document';
  26.  
  27. # Override methods that may be loaded from Data::Grove::Parent.  In
  28. # XML::Grove, `root' and `rootpath' refer to the root _element_ of the
  29. # grove, not the document that contains it.
  30.  
  31. # Note: this routine specifically sets $value and does last instead of
  32. # returning $child immediately because there is a bug in Perl 5.005 that
  33. # causes the returned value to disappear
  34. sub root {
  35.     my $self = shift;
  36.  
  37.     if (@_) {
  38.     return $self->{Contents} = [ shift ];
  39.     } else {
  40.     my $value = undef;
  41.  
  42.     foreach my $child (@{$self->{Contents}}) {
  43.         if ($child->isa('XML::Grove::Element')) {
  44.         $value = $child;
  45.         last;
  46.         }
  47.     }
  48.  
  49.     return $value;
  50.     }
  51. }
  52.  
  53. sub rootpath {
  54.     return;
  55. }
  56.  
  57. package XML::Grove::Element;
  58. use vars qw{ @ISA $type_name };
  59. @ISA = qw{XML::Grove};
  60. $type_name = 'element';
  61.  
  62. package XML::Grove::PI;
  63. use vars qw{ @ISA $type_name };
  64. @ISA = qw{XML::Grove};
  65. $type_name = 'pi';
  66.  
  67. package XML::Grove::Entity::External;
  68. use vars qw{ @ISA $type_name };
  69. @ISA = qw{XML::Grove};
  70. $type_name = 'external_entity';
  71.  
  72. package XML::Grove::Entity::SubDoc;
  73. use vars qw{ @ISA $type_name };
  74. @ISA = qw{XML::Grove};
  75. $type_name = 'subdoc_entity';
  76.  
  77. package XML::Grove::Entity::SGML;
  78. use vars qw{ @ISA $type_name };
  79. @ISA = qw{XML::Grove};
  80. $type_name = 'sgml_entity';
  81.  
  82. package XML::Grove::Entity;
  83. use vars qw{ @ISA $type_name };
  84. @ISA = qw{XML::Grove};
  85. $type_name = 'entity';
  86.  
  87. package XML::Grove::Notation;
  88. use vars qw{ @ISA $type_name };
  89. @ISA = qw{XML::Grove};
  90. $type_name = 'notation';
  91.  
  92. package XML::Grove::Comment;
  93. use vars qw{ @ISA $type_name };
  94. @ISA = qw{XML::Grove};
  95. $type_name = 'comment';
  96.  
  97. package XML::Grove::SubDoc;
  98. use vars qw{ @ISA $type_name };
  99. @ISA = qw{XML::Grove};
  100. $type_name = 'subdoc';
  101.  
  102. package XML::Grove::Characters;
  103. use vars qw{ @ISA $type_name };
  104. @ISA = qw{XML::Grove};
  105. $type_name = 'characters';
  106.  
  107. package XML::Grove::CData;
  108. use vars qw{ @ISA $type_name };
  109. @ISA = qw{XML::Grove};
  110. $type_name = 'cdata';
  111.  
  112. package XML::Grove::ElementDecl;
  113. use vars qw{ @ISA $type_name };
  114. @ISA = qw{XML::Grove};
  115. $type_name = 'element_decl';
  116.  
  117. package XML::Grove::AttListDecl;
  118. use vars qw{ @ISA $type_name };
  119. @ISA = qw{XML::Grove};
  120. $type_name = 'attlist_decl';
  121.  
  122. 1;
  123.  
  124. __END__
  125.  
  126. =head1 NAME
  127.  
  128. XML::Grove - Perl-style XML objects
  129.  
  130. =head1 SYNOPSIS
  131.  
  132.  use XML::Grove;
  133.  
  134.  # Basic parsing and grove building
  135.  use XML::Grove::Builder;
  136.  use XML::Parser::PerlSAX;
  137.  $grove_builder = XML::Grove::Builder->new;
  138.  $parser = XML::Parser::PerlSAX->new ( Handler => $grove_builder );
  139.  $document = $parser->parse ( Source => { SystemId => 'filename' } );
  140.  
  141.  # Creating new objects
  142.  $document = XML::Grove::Document->new ( Contents => [ ] );
  143.  $element = XML::Grove::Element->new ( Name => 'tag',
  144.                                        Attributes => { },
  145.                        Contents => [ ] );
  146.  
  147.  # Accessing XML objects
  148.  $tag_name = $element->{Name};
  149.  $contents = $element->{Contents};
  150.  $parent = $element->{Parent};
  151.  $characters->{Data} = 'XML is fun!';
  152.  
  153. =head1 DESCRIPTION
  154.  
  155. XML::Grove is a tree-based object model for accessing the information
  156. set of parsed or stored XML, HTML, or SGML instances.  XML::Grove
  157. objects are Perl hashes and arrays where you access the properties of
  158. the objects using normal Perl syntax:
  159.  
  160.   $text = $characters->{Data};
  161.  
  162. =head2 How To Create a Grove
  163.  
  164. There are several ways for groves to come into being, they can be read
  165. from a file or string using a parser and a grove builder, they can be
  166. created by your Perl code using the `C<new()>' methods of
  167. XML::Grove::Objects, or databases or other sources can act as groves.
  168.  
  169. The most common way to build groves is using a parser and a grove
  170. builder.  The parser is the package that reads the characters of an
  171. XML file, recognizes the XML syntax, and produces ``events'' reporting
  172. when elements (tags), text (characters), processing instructions, and
  173. other sequences occur.  A grove builder receives (``consumes'' or
  174. ``handles'') these events and builds XML::Grove objects.  The last
  175. thing the parser does is return the XML::Grove::Document object that
  176. the grove builder created, with all of it's elements and character
  177. data.
  178.  
  179. The most common parser and grove builder are XML::Parser::PerlSAX (in
  180. libxml-perl) and XML::Grove::Builder.  To build a grove, create the
  181. grove builder first:
  182.  
  183.   $grove_builder = XML::Grove::Builder->new;
  184.  
  185. Then create the parser, passing it the grove builder as it's handler:
  186.  
  187.   $parser = XML::Parser::PerlSAX->new ( Handler => $grove_builder );
  188.  
  189. This associates the grove builder with the parser so that every time
  190. you parse a document with this parser it will return an
  191. XML::Grove::Document object.  To parse a file, use the `C<Source>'
  192. parameter to the `C<parse()>' method containing a `C<SystemId>'
  193. parameter (URL or path) of the file you want to parse:
  194.  
  195.   $document = $parser->parse ( Source => { SystemId => 'kjv.xml' } );
  196.  
  197. To parse a string held in a Perl variable, use the `C<Source>'
  198. parameter containing a `C<String>' parameter:
  199.  
  200.   $document = $parser->parse ( Source => { String => $xml_text } );
  201.  
  202. The following are all parsers that work with XML::Grove::Builder:
  203.  
  204.   XML::Parser::PerlSAX (in libxml-perl, uses XML::Parser)
  205.   XML::ESISParser      (in libxml-perl, uses James Clark's `nsgmls')
  206.   XML::SAX2Perl        (in libxml-perl, translates SAX 1.0 to PerlSAX)
  207.  
  208. Most parsers supply more properties than the standard information set
  209. below and XML::Grove will make available all the properties given by
  210. the parser, refer to the parser documentation to find out what
  211. additional properties it may provide.
  212.  
  213. Although there are not any available yet (August 1999), PerlSAX filters
  214. can be used to process the output of a parser before it is passed to
  215. XML::Grove::Builder.  XML::Grove::PerlSAX can be used to provide input
  216. to PerlSAX filters or other PerlSAX handlers.
  217.  
  218. =head2 Using Groves
  219.  
  220. The properties provided by parsers are available directly using Perl's
  221. normal syntax for accessing hashes and arrays.  For example, to get
  222. the name of an element:
  223.  
  224.   $element_name = $element->{Name};
  225.  
  226. By convention, all properties provided by parsers are in mixed case.
  227. `C<Parent>' properties are available using the
  228. `C<Data::Grove::Parent>' module.
  229.  
  230. The following is the minimal set of objects and their properties that
  231. you are likely to get from all parsers:
  232.  
  233. =head2 XML::Grove::Document
  234.  
  235. The Document object is parent of the root element of the parsed XML
  236. document.
  237.  
  238. =over 12
  239.  
  240. =item Contents
  241.  
  242. An array containing the root element.
  243.  
  244. =back
  245.  
  246. A document's `Contents' may also contain processing instructions,
  247. comments, and whitespace.
  248.  
  249. Some parsers provide information about the document type, the XML
  250. declaration, or notations and entities.  Check the parser
  251. documentation for property names.
  252.  
  253. =head2 XML::Grove::Element
  254.  
  255. The Element object represents elements from the XML source.
  256.  
  257. =over 12
  258.  
  259. =item Parent
  260.  
  261. The parent object of this element.
  262.  
  263. =item Name
  264.  
  265. A string, the element type name of this element
  266.  
  267. =item Attributes
  268.  
  269. A hash of strings or arrays
  270.  
  271. =item Contents
  272.  
  273. An array of elements, characters, processing instructions, etc.
  274.  
  275. =back
  276.  
  277. In a purely minimal grove, the attributes of an element will be plain
  278. text (Perl scalars).  Some parsers provide access to notations and
  279. entities in attributes, in which case the attribute may contain an
  280. array.
  281.  
  282. =head2 XML::Grove::Characters
  283.  
  284. The Characters object represents text from the XML source.
  285.  
  286. =over 12
  287.  
  288. =item Parent
  289.  
  290. The parent object of this characters object
  291.  
  292. =item Data
  293.  
  294. A string, the characters
  295.  
  296. =back
  297.  
  298. =head2 XML::Grove::PI
  299.  
  300. The PI object represents processing instructions from the XML source.
  301.  
  302. =over 12
  303.  
  304. =item Parent
  305.  
  306. The parent object of this PI object.
  307.  
  308. =item Target
  309.  
  310. A string, the processing instruction target.
  311.  
  312. =item Data
  313.  
  314. A string, the processing instruction data, or undef if none was supplied.
  315.  
  316. =back
  317.  
  318. In addition to the minimal set of objects above, XML::Grove knows
  319. about and parsers may provide the following objects.  Refer to the
  320. parser documentation for descriptions of the properties of these
  321. objects.
  322.  
  323.   XML::Grove::
  324.   ::Entity::External  External entity reference
  325.   ::Entity::SubDoc    External SubDoc reference (SGML)
  326.   ::Entity::SGML      External SGML reference (SGML)
  327.   ::Entity            Entity reference
  328.   ::Notation          Notation declaration
  329.   ::Comment           <!-- A Comment -->
  330.   ::SubDoc            A parsed subdocument (SGML)
  331.   ::CData             A CDATA marked section
  332.   ::ElementDecl       An element declaration from the DTD
  333.   ::AttListDecl       An element's attribute declaration, from the DTD
  334.  
  335. =head1 METHODS
  336.  
  337. XML::Grove by itself only provides one method, new(), for creating new
  338. XML::Grove objects.  There are Data::Grove and XML::Grove extension
  339. modules that give additional methods for working with XML::Grove
  340. objects and new extensions can be created as needed.
  341.  
  342. =over 4
  343.  
  344. =item $obj = XML::Grove::OBJECT->new( [PROPERTIES] )
  345.  
  346. `C<new>' creates a new XML::Grove object with the type I<OBJECT>, and
  347. with the initial I<PROPERTIES>.  I<PROPERTIES> may be given as either
  348. a list of key-value pairs, a hash, or an XML::Grove object to copy.
  349. I<OBJECT> may be any of the objects listed above.
  350.  
  351. =back
  352.  
  353. This is a list of available extensions and the methods they provide
  354. (as of Feb 1999).  Refer to their module documentation for more
  355. information on how to use them.
  356.  
  357.   XML::Grove::AsString
  358.     as_string       return portions of groves as a string
  359.     attr_as_string  return an element's attribute as a string
  360.  
  361.   XML::Grove::AsCanonXML
  362.     as_canon_xml    return XML text in canonical XML format
  363.  
  364.   XML::Grove::PerlSAX
  365.     parse           emulate a PerlSAX parser using the grove objects
  366.  
  367.   Data::Grove::Parent
  368.     root            return the root element of a grove
  369.     rootpath        return an array of all objects between the root
  370.                     element and this object, inclusive
  371.  
  372.     Data::Grove::Parent also adds `C<Parent>' and `C<Raw>' properties
  373.     to grove objects.
  374.  
  375.   Data::Grove::Visitor
  376.     accept          call back a subroutine using an object type name
  377.     accept_name     call back using an element or tag name
  378.     children_accept for each child in Contents, call back a sub
  379.     children_accept_name  same, but using tag names
  380.     attr_accept     call back for the objects in attributes
  381.  
  382.   XML::Grove::IDs
  383.     get_ids         return a list of all ID attributes in grove
  384.  
  385.   XML::Grove::Path
  386.     at_path         $el->at_path('/html/body/ul/li[4]')
  387.  
  388.   XML::Grove::Sub
  389.     filter          run a sub against all the objects in the grove
  390.  
  391. =head1 WRITING EXTENSIONS
  392.  
  393. The class `C<XML::Grove>' is the superclass of all classes in the
  394. XML::Grove module.  `C<XML::Grove>' is a subclass of `C<Data::Grove>'.
  395.  
  396. If you create an extension and you want to add a method to I<all>
  397. XML::Grove objects, then create that method in the XML::Grove
  398. package.  Many extensions only need to add methods to
  399. XML::Grove::Document and/or XML::Grove::Element.
  400.  
  401. When you create an extension you should definitly provide a way to
  402. invoke your module using objects from your package too.  For example,
  403. XML::Grove::AsString's `C<as_string()>' method can also be called
  404. using an XML::Grove::AsString object:
  405.  
  406.   $writer= new XML::Grove::AsString;
  407.   $string = $writer->as_string ( $xml_object );
  408.  
  409. =head1 AUTHOR
  410.  
  411. Ken MacLeod, ken@bitsko.slc.ut.us
  412.  
  413. =head1 SEE ALSO
  414.  
  415. perl(1), XML::Grove(3)
  416.  
  417. Extensible Markup Language (XML) <http://www.w3c.org/XML>
  418.  
  419. =cut
  420.